import psycopg2
from updates import *

conn = psycopg2.connect("dbname='gestion' user='sistemaslis' host='localhost' password='sistemaslis'");
cur = conn.cursor()
CREATED = True
try:
  cur.execute("""SELECT id FROM updates order by id asc""")
except:
  conn.rollback()
  cur.execute("""CREATE TABLE updates (id integer, PRIMARY KEY(id) )""")
  print "tabla updates creada"
  conn.commit()
  CREATED = False
  
if not CREATED:
    cur.execute("""SELECT id FROM updates order by id asc""")
rows = cur.fetchall()
tam = len(rows)-1
last_update_id = 0
if tam+1 != 0:
  last_update_id = rows[tam][0]
try:
  for i in range(last_update_id +1 ,10000):
    exec("update_%s(conn)" % i)
    print " ejecuta funcion de update numero: %s" % i
    cur.execute("""INSERT INTO updates(id)  VALUES (%s);""" % i)
    conn.commit()
except:
    print "cannot update any more"